package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.util.EatShowSetting;

import java.util.ArrayList;
import java.util.List;

public class GudiePageActivity extends BaseActivity {

    private Context mContext;
    private String currentVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.gudiepage);
        mContext = this;
        initView();
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(),0);
            currentVersion = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        ViewPager mViewpage = (ViewPager) findViewById(R.id.gudiePage);
        List<View> viewList = creatViews();
        mViewpage.setAdapter(new ViewPageAdapter(viewList));
    }

    private List<View> creatViews() {
        ArrayList<View> views = new ArrayList<View>();
        View v1 = getLayoutInflater().inflate(R.layout.guide_image,null);
        v1.findViewById(R.id.image).setBackgroundResource(R.drawable.yindaoye1);
        v1.findViewById(R.id.startBtn).setVisibility(View.GONE);

        View v2 = getLayoutInflater().inflate(R.layout.guide_image,null);
        v2.findViewById(R.id.image).setBackgroundResource(R.drawable.yindaoye2);
        v2.findViewById(R.id.startBtn).setVisibility(View.GONE);

        View v3 = getLayoutInflater().inflate(R.layout.guide_image,null);
        v3.findViewById(R.id.image).setBackgroundResource(R.drawable.yindaoye3);
        v3.findViewById(R.id.startBtn).setVisibility(View.GONE);

//        View v4 = getLayoutInflater().inflate(R.layout.guide_image,null);
//        v4.findViewById(R.id.image).setBackgroundResource(R.drawable.yindaoye4);
//        v4.findViewById(R.id.startBtn).setVisibility(View.GONE);

        View v5 = getLayoutInflater().inflate(R.layout.guide_image,null);
        v5.findViewById(R.id.image).setBackgroundResource(R.drawable.yindaoye4);
        ImageButton startBtn = (ImageButton) v5.findViewById(R.id.startBtn);
        startBtn.setVisibility(View.VISIBLE);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,LoginActivity.class);
//                Intent intent = new Intent(mContext,MainActivity.class);
                mContext.startActivity(intent);
                EatShowSetting.getInstance(mContext).setAppVersion(currentVersion);
                finish();
            }
        });
        views.add(v1);
        views.add(v2);
        views.add(v3);
//        views.add(v4);
        views.add(v5);
        return  views;
    }

    class ViewPageAdapter extends PagerAdapter{
        private List<View> mList;

        public ViewPageAdapter(List<View> list) {
            this.mList = list;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mList.get(position),0);
            return mList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }
    }


}
